<?php

function getUserById($BDD_connexion,$login)
{
	
	$request = "SELECT * FROM personne WHERE login = :login";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindParam(':login',$login,PDO::PARAM_STR);
	$prepare->execute();
	$user = $prepare->fetch(PDO::FETCH_OBJ);
	
	return $user;
	
}

function getUserList($BDD_connexion)
{
	
	$users = array();
	
	$request = "SELECT * FROM personne WHERE valide";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->execute();
	while ($user = $prepare->fetch(PDO::FETCH_OBJ))
	{
		$users[] = $user;
	}
	
	return $users;
	
}

function getBanList($BDD_connexion)
{
	
	$users = array();
	
	$request = "SELECT * FROM personne WHERE NOT valide";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->execute();
	while ($user = $prepare->fetch(PDO::FETCH_OBJ))
	{
		$users[] = $user;
	}
	
	return $users;
	
}

function setUserValidity($BDD_connexion,$id,$validity)
{
	
	$request = "UPDATE personne SET valide = :validity WHERE id = :id";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->bindValue(':validity',$validity,PDO::PARAM_INT);
	
	return $prepare->execute();
	
}

function banUser($BDD_connexion,$id)
{
	return setUserValidity($BDD_connexion,$id,0);
}

function unBanUser($BDD_connexion,$id)
{
	return setUserValidity($BDD_connexion,$id,1);
}

function promoteUser($BDD_connexion,$id,$category)
{
	
	$request = "UPDATE personne SET ref_categorie = :category WHERE id = :id";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->bindValue(':category',$category,PDO::PARAM_INT);
	
	return $prepare->execute();
	
}

function deleteUser($BDD_connexion,$id)
{
	
	//On supprime les locations de l'utilisateur ainsi que celles où il était engagé
	$requestLocation = "DELETE FROM location WHERE ref_bien IN (SELECT id_bien FROM bien WHERE ref_proprio = :id) OR ref_personne = :id";
	$prepareLocation = $BDD_connexion->prepare($requestLocation);
	$prepareLocation->bindValue(':id',$id,PDO::PARAM_INT);
	$location = $prepareLocation->execute();
	
	//On supprime les commentaires de l'utilisateur et de ses biens
	$requestCom = "DELETE FROM avis_utilisateur WHERE id_bien IN (SELECT id_bien FROM bien WHERE ref_proprio = :id) OR id_personne = :id";
	$prepareCom = $BDD_connexion->prepare($requestCom);
	$prepareCom->bindValue(':id',$id,PDO::PARAM_INT);
	$com = $prepareCom->execute();
	
	//On supprime les descriptions des biens de l'utilisateur
	$requestDescription = "DELETE FROM description WHERE id_bien IN (SELECT id_bien FROM bien WHERE ref_proprio = :id)";
	$prepareDescription = $BDD_connexion->prepare($requestDescription);
	$prepareDescription->bindValue(':id',$id,PDO::PARAM_INT);
	$description = $prepareDescription->execute();
	
	//On supprime les biens de l'utilisateur
	$requestGood = "DELETE FROM bien WHERE ref_proprio = :id";
	$prepareGood = $BDD_connexion->prepare($requestGood);
	$prepareGood->bindValue(':id',$id,PDO::PARAM_INT);
	$good = $prepareGood->execute();
	
	//On change les intermédiaires des locations où il l'était vers les propriétaires respectifs
	$requestInt = "UPDATE  bien SET ref_intermediaire = ref_proprio WHERE ref_intermediaire = :id";
	$prepareInt = $BDD_connexion->prepare($requestInt);
	$prepareInt->bindValue(':id',$id,PDO::PARAM_INT);
	$int = $prepareInt->execute();
	
	//On supprime la personne de la base de données
	$requestUser = "DELETE FROM personne WHERE id = :id";
	$prepareUser = $BDD_connexion->prepare($requestUser);
	$prepareUser->bindValue(':id',$id,PDO::PARAM_INT);
	$user = $prepareUser->execute();
	
	return ($location && $com && $description && $good && $int && $user);		
}

?>
